package com.example.mydemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.entity.Employee;
import com.example.mydemo.entity.Menu;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.mapper.EmployeeMapper;
import com.example.mydemo.mapper.MenuMapper;
import com.example.mydemo.model.tableVO;
import com.example.mydemo.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syf
 * @since 2025-06-06
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IMenuService iMenuService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @GetMapping("/getEmployeeById")
    @ResponseBody
    public ResponseDTO<Employee> getList(@RequestParam Long id) {
        Employee sysParam = employeeMapper.selectOne(new QueryWrapper<Employee>().eq("id", id).eq("is_delete", 0));
        if (sysParam.getIsDelete()==0L){
            return new ResponseDTO<>(200, "获取信息成功！", sysParam);
        }else {
            return new ResponseDTO<>(200, "获取信息失败！", sysParam);
        }
    }
    @GetMapping("/getEmployeePage")
    public tableVO getsysparmpage(@RequestParam Long page, @RequestParam Long limit, @RequestParam(defaultValue = "") String searchParams) {
        Page<Employee> codepage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        tableVO a = new tableVO();
        if (Objects.equals(searchParams, "")){
            IPage<Employee> sysParamPagePage = employeeMapper.selectPage(codepage, new QueryWrapper<Employee>().eq("is_delete", 0));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }else{
            IPage<Employee> sysParamPagePage = employeeMapper.selectPage(codepage, new QueryWrapper<Employee>().eq("is_delete", 0).like("name", searchParams));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }
    }
    @GetMapping("/getEmployeePageByDep")
    public tableVO getEmployeePageByDep(@RequestParam Long page, @RequestParam Long limit,@RequestParam Long dep, @RequestParam(defaultValue = "") String searchParams) {
        Page<Employee> codepage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        tableVO a = new tableVO();
        if (Objects.equals(searchParams, "")){
            IPage<Employee> sysParamPagePage =
                    employeeMapper
                            .selectPage(codepage, new QueryWrapper<Employee>().eq("is_delete", 0).eq("department_id",dep));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }else{
            IPage<Employee> sysParamPagePage = employeeMapper.selectPage(codepage, new QueryWrapper<Employee>().eq("is_delete", 0).like("name", searchParams));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }
    }
}
